/*-------------------------------
chestnut
Time:2010-11-09
--------------------------------*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <cstdlib>

int main()
{
    using namespace std;

    FILE *fp;
    char str[1024],date[40],time[40],time_pmax[40],time_qmax[40];
    float total_p_f[100],total_q_f[100],max_total_p=0.0,max_total_q=0.0,all_total_p_f=0.0,all_total_q_f=0.0;
    double total_p_d[100],total_q_d[100],all_total_p_d=0.0,all_total_q_d=0.0;
    int i=0;

    fp=fopen("D:\\scada.txt","r");
    if(fp== NULL)
    {
        cout<<"open scada.txt fail"<<endl;
        return 0;
    }
    // while(fgets(str,1024,fp)!= NULL)
    //cout<<"The contxt is"<<str<<endl;
  // for(int i=1;i<97;i++)
 //  {
  //     fgets(str,1024,fp);
  // }
    while(fgets(str,1024,fp)!= NULL)
    {
       // cout << str << endl;
        if((char*)strstr(str,"2010-10-20")==NULL) continue;

       //     system("pause");

       // sscanf(str,"%*[^,],%[^,],%f,%f",time,&total_p_f[i],&total_q_f[i]);
          sscanf(str,"%*[^,],%[^,],%lf,%lf",time,&total_p_d[i],&total_q_d[i]);
          sscanf(str,"%[0-9-],%[0-9:],%f,%f",date,time,&total_p_f[i],&total_q_f[i]);
         // sscanf(str,"%[0-9-],%[0-9:],%f,%f",date,time,&total_p_d[i],&total_q_d[i]);
        if(total_p_f[i]>max_total_p)
        {
            max_total_p= total_p_f[i];
            strcpy(time_pmax, time);
        }
        if(total_q_f[i]>max_total_q)
        {
            max_total_q = total_q_f[i];
            strcpy(time_qmax, time);
        }
        all_total_p_f +=total_p_f[i];
        all_total_q_f +=total_q_f[i];
        all_total_p_d +=total_p_d[i];
        all_total_q_d +=total_q_d[i];
        i++;
    }

    cout<<"The max P appears at:"<<date<<":"<<time_pmax<<",and its value is"<<max_total_p<<endl;
    cout<<"The max Q appears at:"<<date<<":"<<time_qmax<<",and its value is"<<max_total_q<<endl;
    cout<<"The average value of P(float) is:"<<all_total_p_f/96<<endl;
    cout<<"The average value of Q(float) is:"<<all_total_q_f/96<<endl;
    cout<<"The average value of P(double) is:"<<all_total_p_d/96<<endl;
    cout<<"The average value of Q(double) is:"<<all_total_q_d/96<<endl;

}


